package ru.galiauir.items;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class User {
	private Long id;
	private User parent;
	private long totalBonus;
	private long totalPurchase;
	private int lastPurchaseDay;

	public User() {
		this.kids = new ArrayList<User>();
	}

	public User(long id) {
		this();
		this.id = id;
	}

	public List<User> kids;

	public void addKid(long kidId) {
		User kid = new User(kidId);
		kid.setParent(this);
		this.kids.add(kid);
	}

	public void addKid(User kid) {
		kid.setParent(this);
		this.kids.add(kid);
	}

	public int getNumberOfKids() {
		if (this.kids == null || this.kids.isEmpty()) {
			return 0;
		} else {
			return this.kids.size();
		}
	}

	/*
	 * Getters & Setters
	 */
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public User getParent() {
		return parent;
	}

	public void setParent(User parent) {
		this.parent = parent;
	}

	@Override
	public boolean equals(Object o) {
		if (o == null || !(o instanceof User)) {
			return false;
		}

		User u = (User) o;
		if (this.id == u.getId()) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return this.id.hashCode();
	}

	private static final char VERTICAL = '\u2502';
	private static final char SIDE = '\u251c';
	private static final char CORNER = '\u2514';
	private static final char SPACE = '\u0020';
	private static final char HORIZONTAL = '\u2500';

	public void printMyself(PrintWriter stream, int level, String struct, boolean isLast) {
		for (int i = 0; i < level; i++) {
			if (i < level - 1) {
//				try {
					if (struct.charAt(i) == '1') {
						stream.print(VERTICAL);
					} else {
						stream.print(SPACE);
					}
//				} catch (Exception e) {
//					stream.println(("struct.charAt(i) == '1'; struct = " + struct + " level = " + level + " i = " + i));
//				}
				stream.print(SPACE);
				stream.print(SPACE);
			} else {
				if (isLast) {
					stream.print(CORNER);
				} else {
					stream.print(SIDE);
				}
				stream.print(HORIZONTAL);
				stream.print(HORIZONTAL);
			}

		}
		stream.println(String.format("%d (%d; %d)", this.id, this.totalPurchase, this.totalBonus));
		//stream.flush();
		int kidCount = 0;
		if (isLast) {
//			try {
				if (this.kids != null && !this.kids.isEmpty()) {
					struct = struct.substring(0, struct.length() - 1) + "01";
				} else {
					struct = struct.substring(0, struct.length() - 1) + "0";
				}
//			} catch (Exception e) {
//				stream.println("Failed to do: struct = struct.substring(0, struct.length() - 1) + \"0\";".getBytes());
//			}
			//struct += "0";
		} else {
			struct += "1";
		}
		for (User kid : kids) {
			kidCount++;
			kid.printMyself(stream, level + 1, struct, kidCount == kids.size());
		}
	}

	public long getTotalBonus() {
		return totalBonus;
	}

	public void setTotalBonus(long totalBonus) {
		this.totalBonus = totalBonus;
	}

	public long getTotalPurchase() {
		return totalPurchase;
	}

	public void setTotalPurchase(long totalPurchase) {
		this.totalPurchase = totalPurchase;
	}

	public int getLastPurchaseDay() {
		return lastPurchaseDay;
	}

	public void setLastPurchaseDay(int lastPurchaseDay) {
		this.lastPurchaseDay = lastPurchaseDay;
	}

}
